package puzzle.projecteuler.p200;

public class Problem140B {

	/**
	 * A(x) = -(3*x^2+x)/(x^2 + x - 1)
	 *     A(x) = n, x是有理数
	 * <=> 5n^2 + 14n + 1 = m^2
	 * <=> (5n+7)^2 - 44 = 5m^2
	 * 
	 * X^2 - 5Y^2 = 44
	 * 最初的几组解：
	 * 7,1；8,2；13,5；17,7；32,14；43,19；83,37；112,50；217,97；293,131；568,254；767,343
	 * 
	 * 1） X + Y*sqrt(5) = (7 + 1*sqrt(5))*((3+1*sqrt(5))/2)^k, k = 1,3,5, ...
	 * => X = (3X + 5Y)/2
	 *    Y = (X + 3Y)/2
	 * => 
	 * 2） X + Y*sqrt(5) = (8 + 2*sqrt(5))*((3+1*sqrt(5))/2)^k, k = 2,4,6, ...
	 * => X = (3X + 5Y)/2
	 *    Y = (X + 3Y)/2
	 * =>    
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		long x1 = 7;
		long y1 = 1;
		long x2 = 8;
		long y2 = 2;
		int c = 0;
		long sum = 0;
		while (c < 30) {
			long tmp;
			//series 1
			tmp = x1;
			x1 = (3*x1 + 5*y1)/2;
			y1 = (tmp + 3*y1)/2;
			//series 2
			tmp = x2;
			x2 = (3*x2 + 5*y2)/2;
			y2 = (tmp + 3*y2)/2;
			
			if (c%2 == 1) {
				sum += (x1-7)/5;
				System.out.println(x1 + "," + y1 + ":" + (x1*x1-5*y1*y1));			
			} else {
				sum += (x2-7)/5;
				System.out.println(x2 + "," + y2 + ":" + (x2*x2-5*y2*y2));			
			}
			c ++;
		}
		System.out.println(sum);
	}
	
}
